package com.example.myapplication22;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication22.utils.MyHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Context extends AppCompatActivity {
private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     EditText nr;
    SharedPreferences data;
    TextView time1,tit;
    String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.context);

        Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");


        Auto auto = new Auto();
        new Thread(auto).start();//启动自动保存线程


         time1 = findViewById(R.id.time1);
         nr = findViewById(R.id.nr);
         tit=findViewById(R.id.tit);//标题
         Button bc=findViewById(R.id.btn_bc);
        //设置时间

        time1.setText("当前日期："+simpleDateFormat.format(System.currentTimeMillis()+28800000));
        new Auto2().start();//启动动态时间线程
        /*SharedPreferences context = getSharedPreferences("context",MODE_PRIVATE);
        if (context.getString("data",null)!=null){
            nr.setText(context.getString("data",null));
        }*/

        //添加事件
        bc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (nr.getText()!=null){
                    data = getSharedPreferences("context", MODE_PRIVATE);
                    SharedPreferences.Editor edit = data.edit();
                    edit.putString("data",nr.getText().toString());
                    edit.commit();
                    //存入数据库
                    SQLiteDatabase w = new MyHelper(Context.this).getWritableDatabase();
                    ContentValues value = new ContentValues();
                    value.put("userId",String.valueOf(id));
                    value.put("title",tit.getText().toString());
                    value.put("content",nr.getText().toString());
                    value.put("date",time1.getText().toString());
                    long l = w.insert("info",null,value);
                    if (l>0){
                        Toast.makeText(Context.this,"保存成功",Toast.LENGTH_SHORT).show();
                        //添加数据成功跳转到查询页面
                        Intent intent = new Intent(Context.this, List.class);
                        Bundle bundle1 = new Bundle();
                        bundle1.putString("id",id);
                        intent.putExtras(bundle1);

                        startActivity(intent);
                    }



                }

            }
        });

    }

    class Auto implements Runnable{
        @Override
        public void run() {
            while (true){
                try {
                    Thread.sleep(1000*5);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                data = getSharedPreferences("context", MODE_PRIVATE);
                SharedPreferences.Editor edit = data.edit();
                edit.putString("data",nr.getText().toString());
                edit.commit();

            }
        }
    }

    class Auto2 extends Thread{
        @Override
        public void run() {
            while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            time1.setText("当前日期："+simpleDateFormat.format(new Date().getTime()+28800000));

        }}
    }




}
